#Merge plots

alphabet <- c("a","b","c","d","e","f","g","h","i","j","k")
#1) Poverty headcount, distribution
p_legend <- gtable::gtable_filter(ggplotGrob(plot.povts[["PovHeadC"]]), pattern = "guide-box")
p_legend2 <- gtable::gtable_filter(ggplotGrob(plot.reg[["abs"]]), pattern = "guide-box")
p_legend3 <- gtable::gtable_filter(ggplotGrob(plot.dist[["WLD"]]), pattern = "guide-box")
p_legend3.5 <- gtable::gtable_filter(ggplotGrob(plot.dist.wrl[[2030]]), pattern = "guide-box")
p_legend4 <- gtable::gtable_filter(ggplotGrob(FigList[["WorldEmi_CO2"]]), pattern = "guide-box")

pp0.1 <- plot_grid(NULL,plot.povts[["PovHeadC"]] + theme(legend.position="none"), plot.povts[["PovRate"]] + theme(legend.position="none"),p_legend,ncol=4,rel_widths =c(0.5,2,2,1),align = "hv")
pp0.2 <- plot_grid(plot.dist[["WLD"]]+ theme(legend.position="none")+xlab("Income distribution($/year)"),p_legend3,plot.dist.wrl[[2030]]+ theme(legend.position="none")+xlab("Income distribution($/year)"), plot.dist.wrl[[2050]]+ theme(legend.position="none")+xlab("Income distribution($/year)"),p_legend3.5,ncol=5,rel_widths =c(1,0.3,1,1,0.3))
pp0.3 <- plot_grid(plot.reg[["abs"]]+ theme(legend.position="none"),plot.reg[["share"]]+ theme(legend.position="none")+ylab("Regional share (-)"),p_legend2,ncol=3,rel_widths =c(1,1,0.3))
pp0.4 <- plot_grid(FigList[["WorldEmi_CO2"]]+ theme(legend.position="none") +ggtitle(""),FigList[["WorldPol_Cos_GDP_Los_rat"]]+ theme(legend.position="none") +ggtitle(""),FigList[["WorldPrc_Car"]]+ theme(legend.position="none") +ggtitle(""),p_legend4,ncol=4,rel_widths =c(1,1,1,0.3))

pp0 <- plot_grid(pp0.1,pp0.2,pp0.3,pp0.4,ncol=1) +  
  draw_plot_label(label = alphabet[1:10], size = 12,x = c(0,1/2,0,0.33,0.66,0,0.5,0,0.33,0.66), y = c(1, 1,0.75,0.75,0.75,0.5,0.5,0.25,0.25,0.25))
ggsave(pp0, file=paste0("../output/4paper/fig1",".png"), dpi = 250, width=12, height=15,limitsize=FALSE)
ggsave(pp0, file=paste0("../output/vector/fig1.svg"), dpi = 250, width=12, height=15,limitsize=FALSE)

#2)Globalmap
pp1 <- plot_grid(WMlist[["PovRateBaseline2030"]],WMlist[["DifPovRateWB2C2030"]],WMlist[["PovHeadCBaseline2030"]],WMlist[["DifPovHeadCWB2C2030"]],ncol=2,align = "hv") +  
  draw_plot_label(label = alphabet[1:4], size = 12,x = c(0,1/2,0,0.5), y = c(1, 1,0.5,0.5))
ggsave(pp1, file=paste0("../output/4paper/fig2",".png"), dpi = 250, width=12, height=8,limitsize=FALSE)
ggsave(pp1, file=paste0("../output/vector/fig2.svg"), dpi = 250, width=12, height=8,limitsize=FALSE)

#3)Decomposition
p_legend <- gtable::gtable_filter(ggplotGrob(DecomList[["DecAbsPovHeadC"]]), pattern = "guide-box")
pp2.1 <- plot_grid(DecomList[["DecAbsPovHeadC"]] + theme(legend.position="none"),DecomList[["DecShrPovHeadC"]]+ylab("Share (%)") + theme(legend.position="none"),p_legend,ncol=3,rel_widths =c(2,2,0.5),align = "hv") 
pp2.2 <- plot_grid(DecomList[["DecAbsRegionPovHeadC"]] + theme(legend.position="none"),DecomList[["DecShrRegionPovHeadC"]]+ylab("Share (%)") + theme(legend.position="none"),p_legend,ncol=3,rel_widths =c(2,2,0.5),align = "hv") 
pp2 <- plot_grid(pp2.1,pp2.2,plot.price[[1]],ncol=1,rel_heights =c(1,1,2),align = "hv") +  
  draw_plot_label(label = alphabet[1:5], size = 12,x = c(0,0.5,0,0.5,0), y = c(1, 1,0.75,0.75,0.5))
ggsave(pp2, file=paste0("../output/4paper/fig3",".png"), dpi = 250, width=9, height=12,limitsize=FALSE)
ggsave(pp2, file=paste0("../output/vector/fig3.svg"), dpi = 250, width=9, height=12,limitsize=FALSE)

#4) CO2 and poverty
ggsave(CO2_PovComp, file=paste0("../output/4paper/fig4.png"), width=10, height=5,limitsize=FALSE)
ggsave(CO2_PovComp, file=paste0("../output/vector/fig4.svg"), width=10, height=5,limitsize=FALSE)


#X1)Globalmap for 1.5
ppX1 <- plot_grid(WMlist[["DifPovRate1.5C2030"]],WMlist[["DifPovHeadC1.5C2030"]],ncol=2,align = "hv") +  
  draw_plot_label(label = alphabet[1:2], size = 12,x = c(0,1/2), y = c(1, 1))
ggsave(ppX1, file=paste0("../output/4paper/Xfig1",".png"), dpi = 250, width=12, height=4,limitsize=FALSE)
ggsave(ppX1, file=paste0("../output/vector/Xfig1.svg"), dpi = 250, width=12, height=4,limitsize=FALSE)

#X2)Decomposition country
p_legend <- gtable::gtable_filter(ggplotGrob(DecomList[["absWB2C"]]+ theme(legend.position="bottom")), pattern = "guide-box")
ppX1 <- plot_grid(DecomList[["absWB2C"]] + theme(legend.position="none"),DecomList[["shareWB2C"]] + theme(legend.position="none")+ylab("Share (-)"),
                  DecomList[["abs1.5C"]] + theme(legend.position="none"),DecomList[["share1.5C"]] + theme(legend.position="none")+ylab("Share (-)"),ncol=1,align = "hv") 
ppX1.1 <- plot_grid(ppX1,p_legend,ncol=1,rel_heights =c(8,0.5),align = "hv") +
  draw_plot_label(label = alphabet[1:4], size = 12,x = c(0,0,0,0), y = c(1, 0.77,0.54,0.28))
ggsave(ppX1.1, file=paste0("../output/4paper/ExtFig_CountryDec",".png"), dpi = 250, width=10, height=15,limitsize=FALSE)
ggsave(ppX1.1, file=paste0("../output/vector/ExtFig_CountryDec.svg"), dpi = 250, width=10, height=15,limitsize=FALSE)

p_legend <- gtable::gtable_filter(ggplotGrob(plot.dist[["R5ASIA"]]+ theme(legend.position="bottom")), pattern = "guide-box")
ppX1 <- plot_grid(plot.dist[["R5OECD90+EU"]] + theme(legend.position="none")+xlab("Expenditure per capita (2011International$PPP/cap)"),
                  plot.dist[["R5MAF"]] + theme(legend.position="none")+xlab("Expenditure per capita (2011International$PPP/cap)"),plot.dist[["R5ASIA"]] + theme(legend.position="none")+xlab("Expenditure per capita (2011International$PPP/cap)"),
                  plot.dist[["R5LAM"]] + theme(legend.position="none")+xlab("Expenditure per capita (2011International$PPP/cap)"),plot.dist[["R5REF"]] + theme(legend.position="none")+xlab("Expenditure per capita (2011International$PPP/cap)"),
                  ncol=3,align = "hv") 
ppX1.1 <- plot_grid(ppX1,p_legend,ncol=1,rel_heights =c(8,0.5),align = "hv") +
  draw_plot_label(label = alphabet[1:5], size = 12,x = c(0,1/3,2/3,0,1/3), y = c(1, 1,1,0.55,0.55))
ggsave(ppX1.1, file=paste0("../output/4paper/ExtFig_Distribution",".png"), dpi = 250, width=15, height=8,limitsize=FALSE)
ggsave(ppX1.1, file=paste0("../output/vector/ExtFig_Distribution.svg"), dpi = 250, width=10, height=6,limitsize=FALSE)


#X?? Regional Scenarios
for(r in c("World",regionaggset,"IND")){
  p_legend <- gtable::gtable_filter(ggplotGrob(FigList[[paste0(r,"POP")]]+ theme(legend.position="bottom")), pattern = "guide-box")
  ppZ0.1 <- plot_grid(ncol=3,align = "hv",
                      FigList[[paste0(r,"POP")]] + theme(legend.position="none")+ggtitle(""),
                      FigList[[paste0(r,"GDP_per_cap")]]+ theme(legend.position="none")+ggtitle(""),
                      FigList[[paste0(r,"GDP_PPP")]]+ theme(legend.position="none")+ggtitle(""),
                      FigList[[paste0(r,"Gini")]]+ theme(legend.position="none")+ggtitle(""),
                      FigList[[paste0(r,"Emi_CO2")]]+ theme(legend.position="none")+ggtitle(""),
                      FigList[[paste0(r,"Emi_Kyo_Gas")]]+ theme(legend.position="none")+ggtitle(""),
                      FigList[[paste0(r,"Pol_Cos_GDP_Los_rat")]]+ theme(legend.position="none")+ggtitle(""),
                      FigList[[paste0(r,"Prm_Ene")]]+ theme(legend.position="none")+ggtitle(""),
                      FigList[[paste0(r,"Fin_Ene")]]+ theme(legend.position="none")+ggtitle("")
  )
  ppZ1 <- plot_grid(ppZ0.1,p_legend,ncol=1,rel_heights =c(8,0.25),align = "hv") +
    draw_plot_label(label = alphabet[1:9], size = 12,y = c(1,1,1,0.69,0.69,0.69,0.38,0.38,0.38), x = c(0, 1/3,2/3,0, 1/3,2/3,0, 1/3,2/3))
  ggsave(ppZ1, file=paste0("../output/4paper/ExtFig",r,".png"), dpi = 250, width=12, height=12,limitsize=FALSE)
}

